﻿using UBlockly.Scriptable;
using System.Collections.Generic;
using UBlockly.Compotent;
using UnityEngine;

namespace UBlockly.Graphic
{
    /// <summary>
    /// block块结构描述
    /// </summary>
    public struct BlockDescribeData
    {
        private string functionName;
        //private ILFuncType funcType;
        private string resultTypeStr;
        private string[] paramTypesStr;

        public int Classifly;
        public string[] BlockDescs;
        public string FunctionDefineStr;

        public string FunctionName => functionName;
        //public ILFuncType FuncType => funcType;
        public string ResultTypeStr => resultTypeStr;
        public ILVarType ResultType => TranslateHelper.GetILVarTypeByVarTypeName(resultTypeStr);
        public string[] ParamTypesStr => paramTypesStr;
        public System.Enum TargetEnumType => null;

        public BlockDescribeData(string functionDefine, int classify, string[] descs)
        {
            FunctionDefineStr = functionDefine;
            Classifly = classify;
            if (descs != null && descs.Length != 0)
            {
                BlockDescs = new string[descs.Length];
                descs.CopyTo(BlockDescs, 0);
            }
            else
            {
                BlockDescs = new string[1] { "" };
            }
            
            functionName = TranslateHelper.ParseFuncName(FunctionDefineStr);
            resultTypeStr = functionDefine.Substring(0,functionDefine.IndexOf(' '));
            var len = functionDefine.LastIndexOf(')') - functionDefine.IndexOf('(');
            var paramStr = functionDefine.Substring(functionDefine.IndexOf('(')+1, len-1);
            if (string.IsNullOrEmpty(paramStr))
            {
                paramTypesStr = null;
            }
            else
            {
                var arr = new string[1] {paramStr};
                if (paramStr.Contains(","))
                {
                    arr = paramStr.Split(',');
                }

                paramTypesStr = new string[arr.Length];
                for (int i = 0; i < arr.Length; i++)
                {
                    paramTypesStr[i] = arr[i].Substring(0, arr[i].IndexOf(' '));
                }
            }

            //funcType = TranslateHelper.ParseFuncType(FunctionDefineStr,ref resultTypeStr,ref paramTypesStr);
        }

        public string TryGetDesc(int index)
        {
            if (BlockDescs == null || BlockDescs.Length <= index)
                return null;
            return BlockDescs[index];
        }

        public override string ToString()
        {
            //var parmStr = "";
            //if (ParamDescs != null && ParamDescs.Length != 0)
            //{
            //    parmStr = ParamDescs[0];
            //    for (int i = 1; i < ParamDescs.Length; i++)
            //    {
            //        parmStr += "&" + ParamDescs[i];
            //    }
            //}
            //return $"{Classifly}|{BlockDescs[0]}|{(int)SlotTypeShape}|{FunctionDefineStr}|{parmStr}";
            return "";
        }
        public static BlockDescribeData FromString(string str)
        {
            //string[] pp = str.Split('|');
            //if (pp == null || pp.Length != 5)
            //    throw new System.Exception($"BlockDescribeData FromString str error,str:{str}");
            //try
            //{
            //    int classifly = int.Parse(pp[0]);
            //    string descName = pp[1];
            //    BlockSlotShapeType shape = (BlockSlotShapeType)int.Parse(pp[2]);
            //    string defineStr = pp[3];
            //    if (!string.IsNullOrEmpty(pp[4]))
            //    {
            //        string[] paramDescs;
            //        if (pp[4].Contains("&"))
            //            paramDescs = pp[4].Split('&');
            //        else
            //            paramDescs = new string[] { pp[4] };
            //        return new BlockDescribeData(defineStr, classifly, descName, paramDescs, shape);
            //    }
            //    else
            //    {
            //        return new BlockDescribeData(defineStr, classifly, descName, shape);
            //    }
            //}
            //catch(System.Exception e)
            //{
            //    throw new System.Exception($"BlockDescribeData FromString unknow error,str:{str}");
            //}
            return default; 
        }
    }

    /// <summary>
    /// 侧栏序列化的文件
    /// 运行时的侧栏可保存为文本，以供下次进来时更新侧栏
    /// </summary>
    public class SideBarText
    {
        public BlockDescribeData[] GetTotalBlocks()
        {
            return null;
        }
        public ClassiflyItem[] GetSideClassifly()
        {
            return null;
        }

        public static SideBarText FromRuntimeSideBar(ISideBarHandler sideBar)
        {
            return null;
        }
    }

    /// <summary>
    /// 块的序列化文本
    /// </summary>
    public class BlockText
    {
        public string BText;
        public string[] ParamBTexts;

    }

    public class BlockBagText
    {
        string bagDescName;
        List<BlockText> blockTextList;
    }
}
